<script setup lang="ts">
import { ref } from 'vue';
import Taro, { useDidShow } from '@tarojs/taro';

import Page from '@/components/common/Page.vue';
import StepItem from '../enroll/components/StepItem.vue';
import ContentTip from '@/pages/index/components/ContentTip.vue';
import SettledResult from '@/pages/merchant/components/SettledResult.vue';

import ApiMerchant from '@/api/merchant';
import useSubscribeMessage from '@/hooks/useSubscribeMessage';
import useFetch from '@/hooks/useFetch';

import { TMerchantInfo } from '@/global';

// 商户入驻详情
const { data, run } = useFetch<TMerchantInfo>(ApiMerchant.getAppMerchantApplyDetail, {
  afterFetch: (data: any) => data.merchant_apply_detail
});

useDidShow(run);

const subscribe = useSubscribeMessage("settled");
const showSubscribeBtn = ref(true);

const handleSubscribe = async () => {
  const rejectd = await subscribe();

  console.log("rejectd", rejectd);
  Taro.showToast({
    icon: rejectd ? "error" : 'success',
    title: rejectd ? '开启失败' : '开启成功'
  });

  showSubscribeBtn.value = !!rejectd;
};


const handleBtnTap = () => {
  switch (data.value.audit_status) {
    case 0: handleSubscribe(); break;
    case 2: Taro.navigateTo({ url: '/packageMerchant/pages/settled/index' });
  }
};
</script>

<template>
  <Page height="260rpx">
    <view class="mt-3 flex justify-around">
      <StepItem type="success" title="填写信息" />
      <StepItem type="success" title="提交成功" />
      <StepItem :type="data?.audit_status == 1 ? 'success' : data?.audit_status == 2 ? 'fail' : 'waiting'"
                :title="data?.audit_status == 1 ? '审核通过' : data?.audit_status == 2 ? '不通过' : '等待审核结果'" />
    </view>

    <view v-if="data?.audit_status == 1" class="pt-6">
      <ContentTip congrat>
        <view class="text-[40px] font-bold text-dark text-center">
          <view>恭喜您</view>
          <view>入驻成功！</view>
        </view>
      </ContentTip>
    </view>

    <view v-else-if="data"
          class="mt-[200px]">
      <SettledResult :source="data" :hidden-btn="!showSubscribeBtn" @btn-tap="handleBtnTap" />
    </view>
  </Page>
</template>